package com.hnevc.xiaoming.shijianactivity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.hnevc.xiaoming.R;

public class KeyListenerActivity extends AppCompatActivity implements View.OnClickListener, View.OnKeyListener {

    private ImageView img_1;
    private ImageView img_2;
    private TextView tv_view;
    private ImageView[] imageViews = new ImageView[2];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key_listener);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {



    }

    private void initData() {



    }

    private void initView() {
        imageViews[0] = (ImageView) findViewById(R.id.img_1);
        imageViews[1] = (ImageView) findViewById(R.id.img_2);
        tv_view = (TextView) findViewById(R.id.tv_view);
        for (ImageView imageView:imageViews){
            imageView.setOnClickListener(this);
            imageView.setOnKeyListener(this);
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.img_1:
                tv_view.setText("你点击了按钮A");
                break;
            case R.id.img_2:
                tv_view.setText("你点击了按钮B");
                break;
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_A:
                imageViews[0].performClick();
                imageViews[0].requestFocus();
                break;
            case KeyEvent.KEYCODE_B:
                imageViews[1].performClick();
                imageViews[1].requestFocus();
                break;
        }
        return false;
    }
}